package Model;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Observable;

import javax.swing.Timer;

public class Wereld extends Observable {
	private ArrayList<Radar> radarlijst;
	private Helicopter helicopter;
	private Timer timer;
	private Ellipse2D doel, egg;
	private boolean win = false;
	private boolean eggs = false;

	public Wereld() {
		doel = new Ellipse2D.Double(725, -25, 100, 100);
		egg = new Ellipse2D.Double(0, 0, 100, 100);
		radarlijst = new ArrayList<Radar>();
		helicopter = new Helicopter(10, 700);
		timer = new Timer(10, new TimerHandler());
		timer.start();
	}

	public void addRadar(double x, double y, double withZicht,
			double heightZicht, double withManouvreerbaar,
			double heightManouvreerbaar) {
		Radar radar = new Radar(x, y, withZicht, heightZicht,
				withManouvreerbaar, heightManouvreerbaar);
		radarlijst.add(radar);
	}

	public void step() {
		helicopter.setManouvreerbaar(false);
		helicopter.setZichtbaar(false);
		for (Radar radar : radarlijst) {
			radar.setManouvreerbaar(false);
			radar.setZichtbaar(false);
			if (radar
					.checkZichtbaar(helicopter.getPosX(), helicopter.getPosY())) {
				helicopter.setZichtbaar(true);
				radar.setZichtbaar(true);
			}
			if (radar.checkManouvreerbaar(helicopter.getPosX(), helicopter
					.getPosY())) {
				helicopter.setManouvreerbaar(true);
				radar.setManouvreerbaar(true);
			}
		}
		if (doel.contains(helicopter.getPosX(), helicopter.getPosY())) {
			win = true;
		}
		if(egg.contains(helicopter.getPosX(), helicopter.getPosY()))
		{
			eggs = true;
			helicopter.stop(1);
		}
		setChanged();
		notifyObservers("step");
	}

	public void move(double x, double y) {
		helicopter.move(x, y);
		setChanged();
		notifyObservers("move");
	}

	public boolean isWin() {
		return win;
	}
	
	public boolean isEgg()
	{
		return eggs;
	}

	public ArrayList<Radar> getRadarlijst() {
		return radarlijst;
	}

	public Helicopter getHelicopter() {
		return helicopter;
	}

	public Ellipse2D getDoel() {
		return doel;
	}

	class TimerHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			step();
			helicopter.step();
		}
	}

	public void stopHeli() {
		helicopter.stop(0.1);
		setChanged();
		notifyObservers("move");
	}
}
